package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.constant.CommonConstant;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService{

    @Autowired
    private LoginUserUtil loginUserUtil;
    @Override
    public Employee getByIdFromResponse() {
        Object id = loginUserUtil.getBackendUser();
        if(id == null || id.toString().equals("")){
            return null;
        }
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,Long.parseLong(id.toString()));
        return getOne(queryWrapper);
    }

    @Override
    public boolean edit(Employee employee) {

        return updateById(employee);
    }

    @Override
    public boolean add(Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex(CommonConstant.MYSQL_EMPLOYEE_PASSWORD_DEFAULT.getBytes()));
        return save(employee);
    }
}
